package com.ruins.ping.producer;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.lang.management.ManagementFactory;

@Slf4j
@Service
@RequiredArgsConstructor
public class LogSender {

    private final KafkaTemplate<String, String> kafkaTemplate;

    public void sendLog(String logMessage) {
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String pid = name.split("@")[0];

        JSONObject sendData = new JSONObject();
        sendData.put("logMessage", logMessage);
        sendData.put("pid", pid);

        kafkaTemplate.send("logTopic", sendData.toJSONString());
    }
}
